﻿using System;
using System.Collections.Generic;
using System.Text;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using Runda.EShop.Dal;
using System.Data;

namespace Runda.EShop.Bll
{
    public class MasterDataBll:BllBase
    {  
        #region Field and Property

        private string CacheName_GetAllProductCategory = "GetAllProductCategory"; 
        #endregion

        #region Constructor

        public MasterDataBll()
        {
            _cacheName = "MasterDataCacheManager";
        }
        #endregion

        #region Product Category
        public List<BasProductCateEntity> LoadAllProductCategory()
        {
            if (base.ExistsCacheData(CacheName_GetAllProductCategory))
            {
                return GetCacheData(CacheName_GetAllProductCategory) as List<BasProductCateEntity>;
            }
            else
            {
                BasProductCateDal dal = new BasProductCateDal();
                //List<BasProductCateEntity> list = dal.GetAll<BasProductCateEntity>(string.Empty);
                List<BasProductCateEntity> list = dal.GetAll<BasProductCateEntity>("CateType=2");

                AddCache(CacheName_GetAllProductCategory, list);

                return list;
            }
        }
        #endregion

        #region Product
        /// <summary>
        /// 同步产品
        /// </summary>
        public void ProductSync()
        {
            BasProductDal dal = new BasProductDal();
            dal.ProductSync();
        }

        public void ProductCateSync()
        {
            BasProductCateDal dal = new BasProductCateDal();
            dal.ProductCateSync();
        }

        public void EntAddrSync()
        {
            BasProductDal dal = new BasProductDal();
            dal.EntAddrSync();

        }
        public List<BasProductEntity> GetProductListByCate(string CateCode, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            BasProductDal dal = new BasProductDal();
            DataTable dt = dal.GetProductListByCate(CateCode, OrderBy,PageSize,PageIndex,out RecordCount);

            return this.BuildEntityListFromDataTable<BasProductEntity>(dt);
        }

        public List<BasProductEntity> GetProductListByCate(string CateCode,int CateType, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            BasProductDal dal = new BasProductDal();
            DataTable dt = dal.GetProductListByCate(CateCode,CateType, OrderBy, PageSize, PageIndex, out RecordCount);

            return this.BuildEntityListFromDataTable<BasProductEntity>(dt);
        }

        public List<BasProductEntity> GetProductListByCustomer(Guid CustomerID, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            BasProductDal dal = new BasProductDal();
            DataTable dt = dal.GetProductListByCustomer(CustomerID, OrderBy, PageSize, PageIndex, out RecordCount);

            return this.BuildEntityListFromDataTable<BasProductEntity>(dt);
        }

        public List<BasProductEntity> GetProductListByCustomer(Guid CustomerID,int CateType, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            BasProductDal dal = new BasProductDal();
            DataTable dt = dal.GetProductListByCustomer(CustomerID,CateType, OrderBy, PageSize, PageIndex, out RecordCount);

            return this.BuildEntityListFromDataTable<BasProductEntity>(dt);
        }

        public List<BasProductEntity> GetProductListByKey(string Key, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            BasProductDal dal = new BasProductDal();
            DataTable dt = dal.GetProductListByKey(Key, OrderBy, PageSize, PageIndex, out RecordCount);

            return this.BuildEntityListFromDataTable<BasProductEntity>(dt);
        }

        public List<BasProductEntity> GetProductListByKey(string Key,string CustomerID, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            BasProductDal dal = new BasProductDal();
            DataTable dt = dal.GetProductListByKey(Key,CustomerID, OrderBy, PageSize, PageIndex, out RecordCount);

            return this.BuildEntityListFromDataTable<BasProductEntity>(dt);
        }
        
        #endregion

        #region Cache
        public void CacheClear()
        {
            this.Flush();
        }
        #endregion
    }
}
